package com.su.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.su.tools.DaoSupport1;
import com.su.tools.PageData;

import java.text.SimpleDateFormat;
import java.util.*;
import net.sf.json.JSONObject;

@Service("menuService")
public class MenuService {

	@Resource(name = "daoSupport1")
	private DaoSupport1 dao;
	private JSONObject json = new JSONObject();
	private PageData retPd;
	private List<PageData> retListPd;

	public JSONObject invoke(PageData pd) throws Exception {

		if (pd.getString("params").equals("M101")) {
			this.M101(pd);
		} else if (pd.getString("params").equals("M102")) {
			this.M102(pd);
		} else if (pd.getString("params").equals("M103")) {
			this.M103(pd);
		} else if (pd.getString("params").equals("M104")) {
			this.M104(pd);
		} else if (pd.getString("params").equals("M105")) {
			this.M105(pd);
		} else {
			json.put("code", "100");
			json.put("msg", "没有找到该业务代码");
			return json;
		}
		return json;
	}

	/**
	 * 获取菜单列表
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void M101(PageData pd) throws Exception {

		retListPd = dao.getList("MenuMapper.menuInfo", pd);
		for (PageData p : retListPd) {
			if ("#".equals(p.get("_parentId"))) {
				p.remove("_parentId");
				p.put("_parentId", "");
			}
		}
		if (retListPd.size() == 0) {
			json.put("code", "1");
			json.put("msg", "没有查询到数据");
		} else {
			json.put("total", retListPd.size());
			json.put("rows", retListPd);
		}
	}

	/**
	 * 保存菜单
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void M102(PageData pd) throws Exception {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNow = sdf.format(d);
		pd.put("CREATE_DATE", dateNow);
		HttpServletRequest request = pd.getRequest();
		HttpSession session = request.getSession();
		String CZRY = session.getAttribute("session_user").toString();
		pd.put("CZRY", CZRY);
		retPd = dao.getOne("MenuMapper.existMenu", pd);
		int rows = 0;
		if (retPd != null) {
			rows = dao.update("MenuMapper.updateMenu", pd);
		} else {
			rows = dao.insert("MenuMapper.insertMenu", pd);
		}

		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}

	/**
	 * 获取菜单信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void M103(PageData pd) throws Exception {
		retPd = dao.getOne("MenuMapper.getMenu", pd);
		if (retPd != null) {
			json.put("status", "true");
			json.put("record", retPd);
		} else {
			json.put("status", "false");
			json.put("record", retPd);
		}
	}

	/**
	 * 删除菜单
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void M104(PageData pd) throws Exception {
		String codeList = pd.getString("codeList");
		String[] ArrayDATA_IDS = codeList.split(",");
		int rows = dao.delete("MenuMapper.delMenu", ArrayDATA_IDS);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}

	/**
	 * 获取菜单编码
	 * 
	 * @param pd
	 * @throws Exception
	 */
	public void M105(PageData pd) throws Exception {
		String parent_code = pd.getString("parent_code");
		String menu_code = "";
		retPd = dao.getOne("MenuMapper.getMenuCode", pd);
		if ("#".equals(parent_code)) {
			if (retPd == null) {
				menu_code = "1001";
			} else {
				menu_code = retPd.get("menu_code").toString();
			}
		} else {
			if (retPd == null) {
				menu_code = parent_code + "01";
			} else {
				menu_code = retPd.get("menu_code").toString();
			}
		}
		json.put("menu_code", menu_code);
	}

}
